package com.artisan.booteventbus.controller;


import com.artisan.booteventbus.domain.MessageEvent;
import com.artisan.booteventbus.bus.EventBus;
import com.artisan.booteventbus.bus.MessageEventProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/messages")
public class MessageController {

    private final EventBus eventBus;
    private final MessageEventProcessor eventProcessor;

    @Autowired
    public MessageController(EventBus eventBus, MessageEventProcessor eventProcessor) {
        this.eventBus = eventBus;
        this.eventProcessor = eventProcessor;
    }

    @RequestMapping("/send")
    public String sendMessage(@RequestParam String message, @RequestParam String channel) {
        MessageEvent event = new MessageEvent(message, channel, new HashMap<>());
        eventProcessor.handle(event); // 异步处理消息
        return "Message is being processed";
    }
}
